<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Vipuser extends Model
{
    protected $table = 'vipuser';
    public $timestamps = false;
    protected $guarded = [
        'id', 'name', 'avatar', 'weixin', 'ispartner', 'off_line_time', 'created_at', 'updated_at'
    ];

    public function getData($search = [], $page = 1, $length = 20){
        $vipuserList = array();
        $vipuserList['pageData'] = array();
        $date = date('Y-m-d H:i:s', time());

        $sql = \DB::table($this->table.' as p')
            ->select('p.id','p.user_id','p.ispartner','p.user_level','u.nick_name as name','u.headimgurl','u.totalPoints','u.presentPoints','u.inviteCount as invitCount','t.nick_name as inviter')
            ->leftjoin('users as u', 'u.id' , '=' , 'p.user_id')
            ->leftjoin('users as t', 'p.inviter_id' , '=' , 't.id')
            ->where('u.inviteCount','>',0)
            ->where('p.ispartner',0)
            ->whereNull('p.deleted_at')
            ->whereNull('u.deleted_at');


        if(isset($search['weixin'])){
            $sql->where('weixin', 'like', '%'.$search['weixin'].'%');
        }
        if(isset($search['name'])){
            $sql->where('u.nick_name', 'like', '%'.$search['name'].'%');
        }

        $vipuserList['pageData']['count']     = $sql->count();
        $vipuserList['pageData']['lastPage']  = ceil($vipuserList['pageData']['count'] / $length);

        $vipuserList['list'] = $sql->orderBy('u.created_at', 'desc')
            ->skip(($page-1) * $length)->take($length)
            ->get();

        return $vipuserList;
    }

    public function saveData($data){
        return \DB::table($this->table)->insert($data);
    }
//添加合伙人
    public function savepartner($data){
        return \DB::table('partner')->insert($data);
    }
    public function setData($id,$data){
        return \DB::table($this->table)->where('user_id',$id)->update($data);
    }

    public function delData($id,$data){
        return \DB::table($this->table)->where('id',$id)->update($data);
    }

}